package com.hjjt.cameralib.fragment

import android.graphics.Color
import android.graphics.Outline
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import androidx.fragment.app.Fragment
import com.hjjt.cameralib.CameraInstance
import com.hjjt.cameralib.listener.LiveCheckListener
import com.hjjt.cameralib.opengl.GLFrameSurface
import com.hjjt.cameralib.opengl.RoundGLSurfaceView
import com.hjjt.cameraplugin.CameraEngineConfig
import com.hjjt.cameraplugin.ImageData

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.cameralib.fragment
 * @ClassName:      PreviewFragment
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/8/15 9:47
 */
class PreviewFragment private constructor() : Fragment() {
    var surfaceView: GLFrameSurface? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        surfaceView = GLFrameSurface(requireContext())
        return surfaceView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.setBackgroundColor(Color.parseColor("#FFFFFFFF"))
        (view as com.hjjt.cameralib.opengl.GLFrameSurface).run {
            CameraInstance.instance.initSurfaceView(this)
        }

//        CameraInstance.instance.showStream()
        CameraInstance.instance.cameraEngine?.run {
            setEventCallBack(object : LiveCheckListener {
                override fun faceIsLive(data: ImageData) {
                    Log.e("GLSurfaceFragment", "faceIsLive: ")
                }

                override fun changePose() {
                    Log.e("GLSurfaceFragment", "changePose: ")
                }

                override fun faceIsUnLive() {
                    Log.e("GLSurfaceFragment", "faceIsUnLive: ")
                }
            })
            startDetect(
                CameraEngineConfig.DetectType.CLOUD_WALK_DEC_TYPE_LIVENESS_CONTINUE,
                60000
            )
        }
    }

    override fun onDestroy() {
//        CameraInstance.instance.closeStream()
        CameraInstance.instance.cameraEngine?.stopDetect()
        super.onDestroy()
    }

    companion object {
        fun newInstance(): PreviewFragment {
            val fragment = PreviewFragment()
            return fragment
        }
    }
}